﻿@page "/Middleware"

@using Kite.Gateway.Application.Contracts
@using Kite.Gateway.Application.Contracts.Dtos.Middleware
@using Mapster
@using Kite.Gateway.Domain.Shared.Enums
@inject ToastService ToastService
@inject IMiddlewareAppService MiddlewareAppService

<div class="row">
    <div class="col-4">
        <BootstrapInputGroup>
            <BootstrapInput @bind-Value="@SearchKw" PlaceHolder="中间件名称/服务端地址" />
            <Button Icon="fa fa-search" IsAsync="true" @onclick="OnSearchAsync"></Button>
        </BootstrapInputGroup>
    </div>
    <div class="col-2">
          <Button OnClick="@OnAddAsync" Color="Color.Primary">添加中间件</Button>
    </div>
</div>
<div class="row" style="margin-top:10px;">
    <table class="table">
      <thead class="table-light">
        <tr>
            <th>中间件名称</th>
            <th>服务端</th>
            <th>通信类型</th>
            <th>执行权重</th>
            <th>状态</th>
            <th>描述</th>
            <th>创建时间</th>
            <th>更新时间</th>
            <th>操作</th>
        </tr>
      </thead>
      <tbody>
          @if(Middlewares!=null&&Middlewares.Any())
          {
              foreach(var item in Middlewares)
              {
                <tr>
                    <td>@item.Name</td>
                    <td>@item.Server</td>
                    <td>
                        @switch(item.SignalType)
                        {
                            case SignalTypeEnum.Http:
                                <div>http</div>
                            break;
                            default:
                                <div>grpc</div>
                            break;
                        }
                    </td>
                    <td>@item.ExecWeight</td>
                    <td>
                        <Switch OnValueChanged="@(async (val)=>{ OnUpdateStateAsync(item.Id,val);await Task.CompletedTask; })" OnColor="Color.Success" @bind-Value="item.UseState" ShowLabel="false" style="max-height:18px;"  ></Switch>
                    </td>
                    <td>@item.Description</td>
                    <td>@item.Created</td>
                    <td>@item.Updated</td>
                    <td>
                        <div class="btn-group" role="group">
                            <Button Color="Color.Info" Size="Size.ExtraSmall" OnClick="@(e=>OnUpdateAsync(item.Id))">编辑</Button>
                            <PopConfirmButton  Size="Size.ExtraSmall" Placement="Placement.Bottom" Color="Color.Danger" ConfirmIcon="fa fa-exclamation-triangle text-danger"
                  ConfirmButtonColor="Color.Danger" Text="删除" Content="确定删除数据吗？" Icon="fa fa-fa fa-fw" IsAsync="true"
                  OnConfirm="@(()=>OnDeleteAsync(item.Id))" />
                        </div>
                    </td>
                </tr>
              }
            }
        </tbody>
    </table>
    
</div>
<div class="row col-12"  style="margin-top:15px;">
    <Pagination PageItems="@PageSize" PageItemsSource="@(new int[] { 10,20 })" TotalCount="@TotalCount" OnPageClick="@OnPageClick" OnPageItemsChanged="@OnPageItemsChanged"></Pagination>
</div>
<Modal @ref="Modal">
    <ModalDialog  IsScrolling="true" Title="添加中间件" IsCentered="true" Size="Size.Large" ShowCloseButton="false">
        <BodyTemplate >
          <NewMiddleware OnClickCallback="OnAddCallbackAsync"></NewMiddleware>
        </BodyTemplate>
    </ModalDialog>
</Modal>

<Modal @ref="UpdateModal">
    <ModalDialog  IsScrolling="true" Title="中间件信息编辑" IsCentered="true" Size="Size.Large" ShowCloseButton="false">
        <BodyTemplate >
           <UpdateMiddleware Model="@UpdateModel" OnClickCallback="OnUpdateCallbackAsync"></UpdateMiddleware>
        </BodyTemplate>
    </ModalDialog>
</Modal>

@code {
    private Modal Modal;

    private Modal UpdateModal;

    public UpdateMiddlewareDto UpdateModel { get; set; }
    //分页数据

    private List<MiddlewareListDto>? Middlewares { get; set; }

    private int TotalCount { get; set; } = 0;
    private int PageSize { get; set; } = 10;
    private int PageIndex { get; set; } = 1;
    private string SearchKw { get; set; } = "";
    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        OnQueryAsync();
        base.OnInitialized();
    }
    #region 中间件编辑更新
    private async void OnUpdateStateAsync(Guid id,bool val)
    {
        var result = await MiddlewareAppService.UpdateUseStateAsync(id, val);
        if (result.Code != 0)
        {
            await ToastService.Warning("系统警告",result.Message);
        }
        else
        {
            await ToastService.Success("系统通知","保存成功");
        }
    }
    private async void OnUpdateAsync(Guid id)
    {
        UpdateModel= await GetAsync(id);
        await UpdateModal.Toggle();
    }
    private async Task<UpdateMiddlewareDto> GetAsync(Guid id)
    {
        var result = await MiddlewareAppService.GetAsync(id);
        if (result.Code != 0 || result.Data == null)
        {
            await ToastService.Warning("系统警告", "数据不存在");
        }
        var model= TypeAdapter.Adapt<UpdateMiddlewareDto>(result.Data);
        return model;
    }
    private async void OnUpdateCallbackAsync()
    {
        await UpdateModal.Close();
        OnQueryAsync();
    }
    #endregion
    #region 新增中间件
    private async void OnAddAsync()
    {
        await Modal.Toggle();
    }
    private async void OnAddCallbackAsync()
    {
        await Modal.Close();
        OnQueryAsync();
    }
    #endregion
    private async Task OnDeleteAsync(Guid id)
    {
        var result= await MiddlewareAppService.DeleteAsync(id);
        if (result.Code == 0)
        {
            OnQueryAsync();
            await ToastService.Success("系统通知","数据删除成功!");
            return;
        }
        await ToastService.Warning("系统警告","数据删除失败");
    }

    private Task OnPageClick(int pageIndex, int pageItems)
    {
        PageIndex = pageIndex;
        OnQueryAsync();
        return Task.CompletedTask;
    }
    private Task OnPageItemsChanged(int pageItems)
    {
        PageIndex=1;
        PageSize = pageItems;
        OnQueryAsync();
        return Task.CompletedTask;
    }

     private void OnSearchAsync()
    {
        PageIndex=1;
        OnQueryAsync();
    }
    private async void OnQueryAsync()
    {
        var result = await MiddlewareAppService.GetListAsync(SearchKw, PageIndex, PageSize);

        if (result.Code == 0)
        {
            Middlewares = result.Data;
            TotalCount = result.Count;
            StateHasChanged();
        }
        else
        {
            await ToastService.Warning("系统警告", "数据加载失败");
        }
    }

}
